﻿#ifndef NETCONNECTION_H
#define NETCONNECTION_H
#include <QWidget>
#include <QLabel>
#include <QUdpSocket>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QTextBrowser>
#include <QTcpServer>
#include <QDataStream>

class NetConnection : public QWidget
{
    Q_OBJECT

public:
    NetConnection(QWidget* parent = nullptr);
    ~NetConnection();

    QHBoxLayout* hbox1;
    QHBoxLayout* hbox2;
    QHBoxLayout* hbox3;
    QVBoxLayout* vbox;

    QPushButton* TobeMainbtn;
    QPushButton* leavebtn;
    QPushButton* sendbtn;
    QPushButton* connectOverbtn;

//    static QString getLocalCarnumber()  {return LineCarType->text();}
    //将数据以地址的方式传递出去
    QStringList* getCarNumberList()
    {
        return &carNumberList;
    }

    void sceneSynicing(QByteArray imageArray);

    //这里也相当于对外的接口，没有人能够通过这个函数获取到net里面的任何东西，只是传入而已
    void LoadtoConnect(QString msg, QString magSocket, QString ipStr1, QString ipStr2);
    void LoadtoDisConnect(QString msgPlug, QString magSocket, QString ipStr1, QString ipStr2);

    //这里提供了判断是否为server的接口
    bool getisAsServer() const
    {
        return  isAsServer;
    }

    void SockethasReturntoOrigin(QString plugNumber,  QString ipStr_PlugCar);

    void onTimetoRequestEveryCar();



private:
    QLineEdit*  linedit;
    QTextBrowser* textbrower;
    QTextBrowser* textbrower_carList;
    QLineEdit* LineCarType;
    QLabel* LabelText;
    QUdpSocket* udpSocket;
    QTcpServer* tcpServer;
    QTcpSocket* TcpClientSocket;
    bool  isAsServer;
    QStringList carNumberList;


private:
    void udpConnectionInit();
    void tcpServerInit();
    void tcpClientInit(QString IPStr);
    QString getLocalIP();
    void handleUdpMsg(QString str, QString IPStr);
    void handleTcpMsgFromClient(QString msgFromClient, QString ClientIP);
    void handleTcpMsgFromServer(QString msgFromServer, QString ServerIP);
    void handleTcpMsgServerSelf(QString msgFromServerSelf);
    void ServerTcpSend(QString msgPlug, QString magSocket, bool group, QString ipStr1, QString ipStr2);
    void ClientTcpSend(QString msg);
    void countCar();
    void ServerRequestSelf();



signals:
    void signalsendCarState(QString carState);
    void singalsendSceneState(QString SceneState);


private slots:
    void slotUdpRead();
    void slotUdpSend();
    void slotTcpRead();
    void slotNewClientConnection();
    void slotClientTcpconnected();
    void slotAllConnectionOver();
    void slotTcpSend();




};
#endif // NETCONNECTION_H
